package com.oa.tools.controller;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.oa.tools.dao.OrgElementDAO;
import com.oa.tools.jobs.DynamicJob;
import com.oa.tools.jobs.SynchronizeJob;
import com.oa.tools.utils.ExceptionUtil;
import com.oa.tools.utils.FileUtil;
import com.oa.tools.utils.SysConfig;

@RestController
@RequestMapping(path = "org")
public class OrgSyncController {

	@Autowired
	SynchronizeJob synchronizeJob;
	
	@Autowired
	OrgElementDAO orgElementDAO;
	
	public static final Logger logger = LoggerFactory.getLogger(DynamicJob.class);
	
	@RequestMapping({ "sync" })
	public Map<String,Object> syncOrg(@RequestBody Map<String,Object> param) throws Exception {
		
		logger.info("received xrxs json message:"+param.toString());
		
		FileUtil.writeTxt2File(param.toString(), SysConfig.getOutPath());
		synchronizeJob.synchronizeWhenCallBack(param);
			
		Map<String,Object> response = new HashMap<>();
		response.put("message", "success");
		return response;
	}
 	
	
	@RequestMapping({ "depts" })
	public String XRXSDepts(){
		
		logger.info("XRXSDepts is started");
			
		try {
			synchronizeJob.synchronizeWhenTimeout();
		} catch (Exception e) {
			e.printStackTrace();
			return ExceptionUtil.getStackTrace(e);
		}
		return "同步完成";
	}
}